(ns main.routes
  (:use compojure.core
        [main views db admin-view manager-view waiter-view]
        ring.util.response
        ring.middleware.session
        [sandbar stateful-session]
        [ring.middleware.params :only (wrap-params)]
        [hiccup.middleware :only (wrap-base-url)])
  (:require [compojure.route :as route]
            [somnium.congomongo :as mongo]
            [compojure.handler :as handler]
            [compojure.response :as response]))

(defn login [passcode]
  (if (== (user-count passcode) 0)
    (do
      (show-error "Unexisting user"))
    (do
      (session-put! :loged-user ((get-user-by-passcode passcode) :id))
      (if (.equals ((user-session) :role) "Administrator")
        (redirect "/admin/articles")
        (if (.equals ((user-session) :role) "Manager")
          (redirect "/manager")
          (if (.equals ((user-session) :role) "Waiter")
            (redirect "/waiter")
            (show-error "Unknown user role"))))
        )))

(defn lock []
  (do
    (destroy-session!)
    (redirect "/")))

(defroutes main-routes
  (GET "/" [] (index-page))
  (GET "/home" [passcode] (login passcode))
  (GET "/lock" [] (lock))
  (POST "/admin/addadministrator" [fname lname passcode] (add-administrator fname lname passcode))
  (GET "/admin/articles" [] (admin-view-articles))
  (GET "/article/:code" [code] (article-view code))
  (POST "/article/:code" [label price instock code quantity sub-category category]
        (admin-view-edit-article label price instock code quantity sub-category category))
  (GET "/user/:id" [id] (user-view id))
  (POST "/user/:id" [id fname lname date-start date-end passcode role] 
        (admin-view-edit-user id fname lname date-start date-end passcode role))
  (GET "/admin/editarticle/:code" [code] (article-edit-view code))
  (GET "/admin/edituser/:id" [id] (user-edit-view id))
  (GET "/admin/categories" [passcode] (admin-view-categories))
  (GET "/admin/users" [] (admin-view-users))
  (GET "/admin/delete:code" [code] (admin-view-delete-article code))
  (GET "/admin/select:category" [category] (set-category-in-session category))
  (POST "/admin/addarticle" [label price instock quantity sub-category category] 
        (admin-view-add-article label price instock quantity sub-category category))
  (POST "/admin/adduser" [fname lname passcode role] (admin-view-add-user fname lname passcode role))
  (GET "/admin/removeuser/:id" [id] (admin-view-remove-user id))
  (GET "/manager" [] (manager-view-articles))
  (GET "/manager/waiters" [] (manager-view-all-waiters))
  (GET "/manager/waitersinshift" [] (manager-view-waiters-in-shift))
  (GET "/manager/select/:cat" [cat] (set-category cat))
  (POST "/manager/adduser" [fname lname passcode] (manager-add-user fname lname passcode))
  (GET "/manager/changeshift/:id" [id] (manager-change-shift id))
  (GET "/manager/allreports" [] (manager-view-all-reports))
  (GET "/manager/todaysreports" [] (manager-view-todays-reports))
  (GET "/manager/reportsof/:id" [id] (manager-view-reports-of id))
  (GET "/waiter" [] (waiter-main-view))
  (GET "/waiter/createbill" [] (create-bill))
  (GET "/waiter/activebill/:id" [id] (set-active-bill id))
  (GET "/waiter/printbill/:id" [id] (print-bill id))
  (GET "/waiter/removebill/:id" [id] (remove-bill id))
  (GET "/waiter/add/:aid/:bid" [aid bid] (add-article-to-bill aid bid))
  (GET "/waiter/remove/:aid/:bid" [aid bid] (remove-article-from-bill aid bid))
  (route/resources "/")
  (route/not-found (page-not-found)))
  
(def app
  (-> (handler/site main-routes)
      (wrap-base-url)
      (wrap-stateful-session)
      (wrap-params)))
